package br.com.reparoja.reparoja;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import br.com.reparoja.reparoja.ui.DrawerFragment;
import br.com.reparoja.reparoja.ui.FindServiceProviderFragment;
import br.com.reparoja.reparoja.ui.MainFragment;

public class MainActivityDrawer extends ActionBarActivity implements FragmentManager.OnBackStackChangedListener, MainFragment.OnFragmentInteractionListener {

    private static final String TAG = "LocationAddress";


    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTheme(R.style.HomeActivity);
        setContentView(R.layout.activity_main_drawer);

        setTaskProperties();

        FragmentManager fm = getFragmentManager();
        fm.addOnBackStackChangedListener(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
                R.string.open_navigation_drawer,
                R.string.close_navigation_drawer);
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true); //<---- added
        mDrawerToggle.syncState();

        Fragment mainFragment = new MainFragment();
        Fragment drawerFragment = new DrawerFragment();
        //Bundle args = new Bundle();
        //args.putString(MyFragment.ARG_PLANET_NUMBER, days[position]);
        // args.putInt(MyFragment.ARG_PLANET_NUMBER, position);
        //fragment.setArguments(args);

        fm.beginTransaction()
                .replace(R.id.mainContent, mainFragment)
                .replace(R.id.drawerList, drawerFragment)
                .commit();



        /*
        android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("Reparo Já!");

        ImageButton imageButton = (ImageButton) mCustomView
                .findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, FindTechSupportActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
*/

    }

    @TargetApi(value = Build.VERSION_CODES.LOLLIPOP)
    private void setTaskProperties() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.reparoja);
            String title = getString(R.string.app_name);
            int bg = getResources().getColor(R.color.light_theme_color);

            ActivityManager.TaskDescription taskDesc = new ActivityManager.TaskDescription(title, bm, bg);
            this.setTaskDescription(taskDesc);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        if (!UsuarioLogado.temUsuarioLogado())
            getMenuInflater().inflate(R.menu.menu_main, menu);
        else
            getMenuInflater().inflate(R.menu.menu_main2, menu);

        return true;
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        menu.clear();
        if (!UsuarioLogado.temUsuarioLogado())
            getMenuInflater().inflate(R.menu.menu_main, menu);
        else
            getMenuInflater().inflate(R.menu.menu_main2, menu);

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        switch (item.getItemId()) {
            case R.id.entrar:
                openEntrar();
                return true;
            case R.id.criarConta:
                openCriarConta();
                return true;
            case R.id.verPerfil:
                openVerPerfil();
                return true;
            default:
                openSobre();
                return true;
        }

    }




    public void openEntrar() {
        Intent intent = new Intent(this, EntrarActivity.class);
        startActivity(intent);
    }

    public void openCriarConta() {

    }

    public void openSobre() {

    }

    public void openVerPerfil() {
        Intent intent = new Intent(this, VerPerfil.class);
        startActivity(intent);
    }

    @Override
    public void onBackStackChanged() {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);

        getSupportActionBar().setTitle(title);
    }

    /** Swaps fragments in the main content view */
    public void onDrawerItemClicked(int position, ListView drawerList) {
        // Create a new fragment and specify the planet to show based on position
        Fragment fragment = new FindServiceProviderFragment();
        /*Bundle args = new Bundle();
        args.putInt(FindServiceProviderFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);
*/
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.mainContent, fragment)
                .commit();

        // Highlight the selected item, update the title, and close the drawer
        drawerList.setItemChecked(position, true);
        setTitle("Encontrar Assistencia");
        mDrawerLayout.closeDrawer(Gravity.START);
    }

};



